package com.geeka.d12.work;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpClientDemo2 {
    public static void main(String[] args) {
        /**
         * 1 客户端可以连接向服务端发送消息
         * 2 客户端连上服务端可以接收到欢迎信息
         */
        Scanner sc = new Scanner(System.in);

        try (Socket socket = new Socket("127.0.0.1", 7777);) {
            //1, 建立连接
            // 创建输入流,接收服务端欢迎信息.
            InputStream ips = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(ips));
            System.out.println(br.readLine());
            while (true) {

                OutputStream ops = socket.getOutputStream();
                PrintStream ps = new PrintStream(ops);
                System.out.print("向服务器发送内容: ");
                String content = sc.next();
                ps.println(content);
                ps.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
